﻿using ERPSystem.Common.Domains.SalesOrderCommom;
using ERPSystem.Common.DTO;
using ERPSystem.Common.Infrastructure.IRepository;
using ERPSystem.Enum.CommonErrorCodes;
using MediatR;
using SalesManagementWrite.API.SalesManagementWrites.SalesOrders;

namespace SalesManagementWrite.API.OutOfStock
{
    public class SellOutMaterialsCreateHander : IRequestHandler<SellOutMaterialsCreateCommand, Result>
    {
        private readonly IBaseRepository<SellOutMaterials> _sellOutMaterialsRepository;

        public SellOutMaterialsCreateHander(IBaseRepository<SellOutMaterials> sellOutMaterialsRepository)
        {
            _sellOutMaterialsRepository = sellOutMaterialsRepository;
        }

        /// <summary>
        /// 处理销售出库物料新增
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(SellOutMaterialsCreateCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            try
            {
                var list = await _sellOutMaterialsRepository.AddRange(request.Materials);
                result.Data = list;
                if (list > 0)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Msg = "添加成功";
                }
                else
                {
                    result.Msg = "添加失败";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
